package com.linxz.ui.spraycarview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;

import com.linxz.ui.R;

import java.util.HashMap;
import java.util.List;


public class SprayItemView extends LinearLayout {


    private HashMap<String,RadioGroup> radioGroups =new HashMap<>();
    private HashMap<String,RadioButton> radioButtons=new HashMap<>();


    private SprayItemData data;
    private boolean isInited=false;

    public SprayItemView(Context context) {
        super(context);
        init(context,null);
    }

    public SprayItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public SprayItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }
    private void init(Context context,AttributeSet attrs){
         setOrientation(VERTICAL);
    }

    public void bindData(LifecycleOwner owner, final SprayItemData data, final SprayPaintCarView carView){
        this.data=data;
        this.data.observe(owner, new Observer<List<SprayItem1>>() {
            @Override
            public void onChanged(@Nullable List<SprayItem1> sprayItem1s) {
                  if(sprayItem1s!=null){
                      if(!isInited){
                          removeAllViews();
                          radioGroups.clear();
                          radioButtons.clear();
                          for(SprayItem1 item1:sprayItem1s){
                              LayoutInflater inflater=LayoutInflater.from(getContext());
                              LinearLayout layout1= (LinearLayout) inflater.inflate(R.layout.view_spary_item1,null,false);
                              TextView tvDirection=layout1.findViewById(R.id.tvDirection);
                              tvDirection.setText(item1.getTitle());
                              LinearLayout layoutItems=layout1.findViewById(R.id.llItems);
                              if(item1.getList()!=null){
                                  for(final SprayItem2 item2:item1.getList()){
                                      LinearLayout layoutItem2= (LinearLayout) inflater.inflate(R.layout.view_spray_item2,null,false);
                                      layoutItems.addView(layoutItem2);
                                      TextView tvPartName=layoutItem2.findViewById(R.id.tvPartName);
                                      tvPartName.setText(item2.getTitle());
                                      RadioGroup group=layoutItem2.findViewById(R.id.rgPart);
                                      group.setTag(item2);
                                      radioGroups.put(item2.getId(),group);
                                      RadioButton rbSpray=group.findViewById(R.id.rbSpray);
                                       RadioButton rbMetal=group.findViewById(R.id.rbMetal);
                                      if(item2.getList()!=null){
                                          if(item2.getList().size()>=1){
                                              SprayItem3 itemSpray=item2.getList().get(0);
                                              rbSpray.setVisibility(View.VISIBLE);
                                              rbSpray.setText(itemSpray.getTitle());
                                              rbSpray.setTag(itemSpray);
                                              radioButtons.put(itemSpray.getId(),rbSpray);
                                          }else {
                                              rbSpray.setVisibility(View.INVISIBLE);
                                          }
                                          if(item2.getList().size()>=2){
                                              SprayItem3 itemMetal=item2.getList().get(1);
                                              rbMetal.setTag(itemMetal);
                                              rbMetal.setVisibility(View.VISIBLE);
                                              rbMetal.setText(itemMetal.getTitle());
                                              radioButtons.put(itemMetal.getId(),rbMetal);
                                          }else {
                                              rbMetal.setVisibility(View.INVISIBLE);
                                          }
                                      }
                                  }
                              }
                              addView(layout1);
                          }
                          isInited=true;
                      }

                      for(SprayItem1 item1:sprayItem1s){
                          for(final SprayItem2 item2:item1.getList()){
                              RadioGroup group= radioGroups.get(item2.getId());
                              group.setOnCheckedChangeListener(null);
                              if(item2.getSelectedId()==null){
                                  group.clearCheck();
                              }else {
                                  RadioButton radioButton=radioButtons.get(item2.getSelectedId());
                                  if(radioButton!=null){
                                      radioButton.setChecked(true);
                                  }
                              }
                              group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                                  @Override
                                  public void onCheckedChanged(RadioGroup group, int checkedId) {
                                      System.out.println("###onCheckedChanged:"+group+" "+checkedId+" "+group.getCheckedRadioButtonId());

                                      if(checkedId==-1){
                                          item2.checkNull();

                                      }else {
                                          String selectedId=null;
                                          RadioButton radioButton= group.findViewById(checkedId);
                                          if(radioButton!=null){
                                              SprayItem3  item3= (SprayItem3) radioButton.getTag();
                                              if(item3!=null){
                                                  selectedId=item3.getId();
                                              }
                                          }
                                          if(Constant.ID_ALL.equals(item2.getId())){
                                                data.checkNull();
                                          }else {
                                              RadioGroup radioGroupAll=radioGroups.get(Constant.ID_ALL);
                                              if(radioGroupAll.getCheckedRadioButtonId()!=-1){
                                                  SprayItem2 allItem2= (SprayItem2) radioGroupAll.getTag();
                                                  allItem2.setSelectedId(null);
                                                  data.checkNull();
                                              }
                                          }
                                          item2.setSelectedId(selectedId);
                                      }
                                      if(carView!=null){
                                          carView.setCurrentDirection(item2.getDirection());
                                      }
                                      if(data!=null){
                                          data.refresh();
                                      }
                                  }
                              });

                          }
                      }
                  }
            }
        });


    }




}
